package com.whfc.wxmp.api.safety.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysUserService;
import com.whfc.safety.dto.*;
import com.whfc.safety.enums.SafetyCheckUserType;
import com.whfc.safety.param.*;
import com.whfc.safety.service.SafetyDangerService;
import com.whfc.wxmp.api.safety.service.WxSafetyDangerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description 危大工程
 * @Author hw
 * @Date 2021-08-20 10:45
 * @Version 1.0
 */
@Service
public class WxSafetyDangerServiceImpl implements WxSafetyDangerService {

    @DubboReference(interfaceClass = SafetyDangerService.class, version = "1.0.0")
    private SafetyDangerService safetyDangerService;


    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @Override
    public PageData<SafetyDangerDTO> list(Integer deptId, Integer state, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerList(deptId, state, pageNum, pageSize);
    }

    @Override
    public PageData<SafetyDangerExtDTO> dangerExt(Integer dangerId, Integer type, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerExt(dangerId, type, pageNum, pageSize);
    }

    @Override
    public void dangerAddExt(SafetyDangerAddExtParam param, String phone) {
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        if (sysUser == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "请联系管理员添加账号");
        }
        param.setUserId(sysUser.getId());
        param.setUserName(sysUser.getNickname());
        safetyDangerService.dangerAddExt(param);
    }

    @Override
    public void dangerEditExt(SafetyDangerEditExtParam param) {
        safetyDangerService.dangerEditExt(param);
    }

    @Override
    public void dangerExtDel(Integer dangerId, Integer extId, Integer type) {
        safetyDangerService.dangerExtDel(dangerId, extId, type);
    }

    @Override
    public SafetyDangerCheckNumDTO dangerCheckNum(Integer dangerId) {
        return safetyDangerService.dangerCheckNum(dangerId);
    }

    @Override
    public PageData<SafetyCheckDTO> dangerCheckList(Integer dangerId, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerCheckList(dangerId, pageNum, pageSize);
    }

    @Override
    public SafetyCheckDescDTO dangerCheckDesc(Integer checkId) {
        return safetyDangerService.dangerCheckDesc(checkId);
    }

    @Override
    public void dangerCheckAdd(SafetyDangerCheckAddParam param, String phone) {
        SysUser sysUser = sysUserService.getUserByPhone(phone);
        if (sysUser == null) {
            throw new BizException(ResultEnum.NOT_FOUND.getCode(), "请联系管理员添加账号");
        }
        SafetyCheckUserParam userParam = new SafetyCheckUserParam();
        userParam.setType(SafetyCheckUserType.REPORTER.getValue());
        userParam.setUserId(sysUser.getId());
        userParam.setUserName(sysUser.getNickname());
        userParam.setUserPhone(sysUser.getPhone());
        param.getCheckUsers().add(userParam);
        Collections.sort(param.getCheckUsers());
        safetyDangerService.dangerCheckAdd(param);
    }

    @Override
    public ListData<SafetyDangerControlDTO> dangerControlList(Integer dangerId) {
        List<SafetyDangerControlDTO> list = safetyDangerService.dangerControlList(dangerId);
        return new ListData<>(list);
    }

    @Override
    public void dangerControlComplete(SafetyDangerControlCompleteParam param) {
        safetyDangerService.dangerControlComplete(param);
    }
}
